package com.ssm.sfzy.controller;

import com.ssm.sfzy.constant.CommonConstant;
import com.ssm.sfzy.entity.Book;
import com.ssm.sfzy.entity.FM;
import com.ssm.sfzy.service.BookService;
import com.ssm.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author song
 * @date 2024年04月01日 11:54
 * @version:1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @GetMapping("/find")
    public Object find(
            @RequestParam(defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
            Book book
    ){
        Map<String, Object> map = bookService.findPage(pageNum, pageSize, book);
        return Result.success(map);
    }

    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable Integer id){
        bookService.findById(id);
        return Result.success(id);
    }

    @PostMapping("/add")
    public Object add(@RequestBody Book book){
        bookService.add(book);
        return Result.success(book);
    }

    @PutMapping("/edit")
    public Object edit(@RequestBody Book book){
        bookService.edit(book);
        return Result.success(book);
    }

    @DeleteMapping("/remove/{id}")
    public Object remove(@PathVariable Integer id){
        bookService.remove(id);
        return Result.success(id);
    }

    @DeleteMapping("/removeBatch/{ids}")
    public Object removeBatch(Integer[] ids){
        bookService.removeBatch(ids);
        return Result.success(ids);
    }

}
